package com.peixunp.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.peixunp.bean.Place;
import com.peixunp.bean.PlaceKey;
import com.peixunp.dao.OrganizationsDao;
import com.peixunp.dao.PlaceDao;
import com.peixunp.service.PlaceService;
import com.peixunp.util.Constants;

public class PlaceServiceImpl implements PlaceService {

	@Autowired
	private PlaceDao placeDao;
	
	@Autowired
	private OrganizationsDao organizationsDao;
	
	@Override
	public List<Place> getAll() throws Exception {
		return placeDao.findAll();
	}
	
	@Override
	public boolean update(Place place) throws Exception {
		return placeDao.update(place);
	}
	
	@Override
	public boolean insert(Place place) throws Exception {
		return placeDao.insert(place);
	}
	
	/**
	 * 生成城市文件,包括城市有多少个学校
	 */
	@Override
	public boolean createCityList(String city_file_real_path) throws Exception {
		List<Place> provinceList = placeDao.getProvinceList();
		OutputStreamWriter out = null;
		for (Place province : provinceList) {
			String jspFileNameString = province.getId()+".jsp";
			List<Place> cityList  = placeDao.getCityList( province.getId() );
			StringBuffer cityTotle  = new StringBuffer();
			String rn = System.getProperty("line.separator");//System.getProperty("line.separator")的意思是 "\r\n"
			cityTotle.append("<%@ page contentType=\"text/html; charset=UTF-8\" %>" + rn );
			cityTotle.append("<h3>");
			cityTotle.append(province.getPname());
			cityTotle.append(":</h3>"+ rn);
			cityTotle.append("<div class=\"city_box\">"+ rn);
			cityTotle.append("<div class=\"defaultTd2ul\">"+ rn);
			cityTotle.append("<ul>"+ rn);
			for (Place city : cityList) {
				int citySchoolNum  = organizationsDao.getCitySchoolNum( city.getId() );
				cityTotle.append("<li><img src=\""+Constants.URL+"/images/arrow.gif\" />");
				cityTotle.append("<a href=\""+Constants.URL+"/city/"+city.getId()+".html\">"+city.getPname()+"(<font color=\"red\">"+citySchoolNum+"</font>间)</a></li>");
				cityTotle.append(rn);
			}
			cityTotle.append("</ul>"+ rn );
			cityTotle.append("</div>"+ rn );
			cityTotle.append("</div>"+ rn );
			try {
				//File.separator是跨平台的路径符
				out = new OutputStreamWriter(new FileOutputStream(city_file_real_path + File.separator + jspFileNameString ),"UTF-8" );
				out.write( cityTotle.toString());
			} catch (Exception e) {
				if(Constants.ISDEBUG){
					e.printStackTrace();
				}
			}finally{
				 if (out != null) {
		                try {
		                	out.flush();
		                	out.close();
		                } catch (IOException ioe) {
		    				if(Constants.ISDEBUG){
		    					ioe.printStackTrace();
		    				}
		                }
		            }
			}
		}
		return true;
	}
	
	@Override
	public Place findByKey(int cityId) throws Exception {
		return placeDao.findByKey( new PlaceKey(cityId) );
	}
	
}
